<template>
  <div class="details">
    <el-page-header @back="$router.push({name:'reviewPlanList'})" content="详情页面"></el-page-header>
    <div class="cotnent">
      <div class="planName border">
        <span>计划：</span>
        {{item.planName}}
      </div>
      <div class="cotnent-box">
        <el-row :gutter="20" class="border">
          <el-col :span="4">
            <span>区域：</span>
            <span
              v-if="item.amzRegionId && amzRegionList.length > 0"
            >{{amzRegionList.find(el => el.amazonRegionId == item.amzRegionId).amzRegionCode}}</span>
          </el-col>
          <el-col :span="4">
            <span>站点：</span>
            <span
              v-if="item.siteId && amzRegionList.length > 0"
            >{{countryList.find(el => el.countryId == item.siteId).alphaCode}}</span>
          </el-col>
          <el-col :span="4">
            <span>账号：</span>
            <span
              v-if="item.accountId && amzRegionList.length > 0"
            >{{accountList.find(el => el.sellerId == item.accountId).sellerName}}</span>
          </el-col>
          <el-col :span="4">
            <span>执行时间间隔：</span>
            {{item.timeInterval}}
          </el-col>
          <el-col :span="4">
            <span>点击量：</span>
            {{item.clickQuantity}}
          </el-col>
        </el-row>
        <el-row :gutter="20" class="border">
          <el-col :span="8">
            <span>下单后时间范围：</span>
            {{item.orderTimeStart | formatDate}}~{{item.orderTimeEnd | formatDate}}
          </el-col>
          <el-col :span="8">
            <span>计划时间：</span>
            {{item.planTimeStart | formatDate}}~{{item.planTimeEnd | formatDate}}
          </el-col>
          <el-col :span="8">
            <span>执行时间范围：</span>
            {{item.rangeStart }}~{{item.rangeEnd }}
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col>
            <span>{{item.asin?'ASIN':'SKU'}}{{item.isInclude?'（包含）':'（不包含）'}}</span>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col>
            <div class="bg">
              <div v-if="item.asin">
                <p v-for="(item,index) in item.asin.split(/[(\r\n)\r\n]+/)" :key="index">{{item}}</p>
              </div>
              <div v-else>
                <p v-for="(item,index) in item.sku.split(/[(\r\n)\r\n]+/)" :key="index">{{item}}</p>
              </div>
            </div>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col>
            <span>排除OrderId</span>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col>
            <div class="bg">
              <p v-for="(item,index) in item.orderId.split(/[(\r\n)\r\n]+/)" :key="index">{{item}}</p>
            </div>
          </el-col>
        </el-row>
      </div>
    </div>
    <div class="button" v-if="checkPwdEnabel && item.planStatus == 1 && item.Check">
      <el-button type="primary" size="mini" @click="clickCheck(2)">审核通过</el-button>
      <el-button type="danger" size="mini" @click="clickCheck(6)">驳回计划</el-button>
    </div>
    <el-table
      v-if="!item.Check"
      :data="tableData"
      v-loading="loading"
      border
      :header-cell-style="getRowClass"
      style="width: 100%"
    >
      <el-table-column prop="taskId" label="任务编号"></el-table-column>
      <el-table-column prop="taskStatus" label="状态">
        <template slot-scope="scope">
          <el-popover
            placement="top-start"
            width="200"
            trigger="hover"
            :content="scope.row.errorReason"
          >
            <span slot="reference">{{scope.row.taskStatus | statusFormat}}</span>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="address" label="任务时间">
        <template slot-scope="scope">
          <p>计划执行时间：{{scope.row.taskStartTime | formatDateHMS}}</p>
          <p>任务完成时间：{{scope.row.taskEndTime | formatDateHMS}}</p>
        </template>
      </el-table-column>
      <el-table-column prop="address" label="执行记录">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="$refs.reviewPlanModal.show(scope.row)">查看详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <review-plan-modal ref="reviewPlanModal"></review-plan-modal>
  </div>
</template>
<script>
import reviewPlanModal from "./reviewPlanModal";
import { selectTaskByPlan, reviewplanCheck } from "@/api/reviewPlanController";
import { formatDate } from "assets/js/date";
import { AmazonAccount, Country, RegionQuery } from "@/api/common";
import { checkPermission } from "@/utils/permissions";
export default {
  components: {
    reviewPlanModal,
  },
  data() {
    return {
      tableData: [],
      loading: true,
      item: this.$route.query,
      statusList: [],
      accountList: [],
      countryList: [],
      amzRegionList: [],
      checkPwdEnabel: checkPermission("system:plan:check"),
    };
  },
  created() {
    this.getData();
    this.getAccount();
    this.getCountry();
    this.getRegionQuery();
  },
  filters: {
    formatDate(time) {
      if (time) {
        var date = new Date(time);
        return formatDate(date, "yyyy-MM-dd");
      }
    },
    formatDateHMS(time) {
      if (time) {
        var date = new Date(time);
        return formatDate(date, "yyyy-MM-dd hh:mm:ss");
      }
    },
    statusFormat(key) {
      switch (key) {
        case 1:
          return "等待执行";
          break;
        case 2:
          return "正在执行";
          break;
        case 3:
          return "执行错误";
          break;
        case 4:
          return "已完成";
          break;
        case 5:
          return "停止";
          break;
      }
    },
  },
  methods: {
    getData() {
      this.loading = true;
      selectTaskByPlan({ planId: this.item.planId }).then((res) => {
        this.tableData = res.data.data.rows;
        this.loading = false;
      });
    },
    getAccount() {
      AmazonAccount().then((res) => {
        this.accountList = res.data.data.data;
      });
    },
    getCountry() {
      Country().then((res) => {
        this.countryList = res.data.data.Country;
      });
    },
    getRegionQuery() {
      RegionQuery().then((res) => {
        this.amzRegionList = res.data.data.data;
      });
    },
    clickCheck(status) {
      if (status === 2) {
        this.$confirm("计划审核通过, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            this.saveReviewplanCheck({
              planId: this.item.planId,
              planStatus: status,
              rejectReason: "",
            });
          })
          .catch(() => {});
      } else {
        this.$prompt("请输入驳回理由", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
        })
          .then(({ value }) => {
            this.saveReviewplanCheck({
              planId: this.item.planId,
              planStatus: status,
              rejectReason: value,
            });
          })
          .catch(() => {});
      }
    },
    saveReviewplanCheck(params) {
      reviewplanCheck(params).then((res) => {
        if (res.data.code === this.ERR_OK) {
          this.$message.success(res.data.msg);
          this.$router.push({ name: "reviewPlanList" });
        } else {
          this.$message.error(res.data.msg);
        }
      });
    },
    // 标题背景颜色
    getRowClass({ row, column, rowIndex, columnIndex }) {
      if (rowIndex === 0) {
        return "background: #EBEEF5 ";
      } else {
        return "";
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.details {
  padding: 15px;
}
.cotnent {
  margin-top: 15px;
  padding: 15px;
  font-size: 16px;
  line-height: 50px;
  color: #000000;
  box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
  .border {
    border-bottom: 1px solid #dddfe4;
  }
  span {
    font-weight: bold;
    color: #666666;
  }
  .bg {
    background: #ebeef5;
    padding: 10px;
    line-height: 26px;
    p {
      display: inline-block;
      visibility: top;
      min-width: 150px;
      padding: 0;
      margin: 0;
      font-size: 14px;
      color: #000000;
    }
  }
}
.button{
  float: right;
  margin-top: 15px;
}
.el-table {
  margin-top: 20px;
  p {
    padding: 0;
    line-height: 0;
    line-height: 16px;
  }
}
</style>